package cn.hotel.controller.worker;

import cn.hotel.entity.User;
import cn.hotel.response.ResponseTool;
import cn.hotel.response.Result;
import cn.hotel.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import cn.hotel.mapper.UserMapper;
import cn.hotel.response.ResponseTool;
import cn.hotel.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping(value = "/op/user")
public class OpUserController {
    @Autowired
    private UserMapper userMapper;
    @Resource
    private UserService userService;

    @PostMapping("/count")
    public Result selectUsersCount(){
        int totalUserCount = userMapper.getUsers();
        return ResponseTool.success(totalUserCount);
    }

    /**
     * 查询所有客户
     * @return
     */
    @PostMapping
    public Result allUser() {

        List<User> users = userService.allUser();
        log.info("users的信息{}" , users);
        if (users == null) {
            return ResponseTool.failed("查询失败!!");
        }
        return ResponseTool.success(users);
    }

    /**
     * 根据id删除用户
     * @param userId
     * @return
     */
    @PostMapping("/delete/{userId}")
    public Result deleteUser(@PathVariable Integer userId) {
        log.info("获取的userId值:{}" , userId);
        int i = userService.deleteByUserId(userId);
        if (i == 0) {
            return ResponseTool.failed("删除失败，删除了0条记录");
        }
        return ResponseTool.success();
    }

    @PostMapping("/deleteS")
    public Result delUsers(@RequestParam Integer[] users){
        log.info("获取users的信息{}" , users);
        if (users == null) {
            ResponseTool.failed("查询失败!!");
        }
        for (int i = 0; i < users.length; i++) {
            System.out.println(users[i]);
            userService.deleteByUserId(users[i]);
        }

        return ResponseTool.success();
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @PostMapping("/add")
    public Result addUser(User user) {
        log.info("获取的user值:{}" , user);
        int i = userService.saveUser(user);
        if (i == 0) {
            return ResponseTool.failed("添加失败!!");
        }
        return ResponseTool.success();
    }


    /**
     * 根据userId查询user
     * @param userId
     * @return
     */
    @PostMapping("/{userId}")
    public Result queryById(@PathVariable Integer userId) {
        log.info("获取的userId值:{}" , userId);
        User user = userService.queryById(userId);
        user.setUserId(userId);
        return ResponseTool.success(user);
    }


    /**
     * 更新用户信息
     * @param user
     * @return
     */
    @PostMapping("/update")
    public Result update(User user) {
        log.info("获取的user的值:{}" , user);
        int i = userService.updateUser(user);
        System.out.println(i+"pppppp");
        return ResponseTool.success();
    }

    /**
     * 根据姓名模糊查询
     * @param name
     * @return
     */
    @PostMapping("/byName")
    public Result getUserByName(@RequestParam String name) {
        log.info("获取到的name:{}" , name);
        List<User> userByName = userService.getUserByName(name);

        if (userByName == null) {
            return ResponseTool.failed("查询失败");
        }
        return ResponseTool.success(userByName);
    }



}
